package pl.lodz.p.pai.order.db;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSession;


import pl.lodz.p.pai.common.IbatisDaoBase;
import pl.lodz.p.pai.order.types.Order;

public class IbatisOrderDao extends IbatisDaoBase implements OrderDao {

	protected DataSource locateDataSource() {
		try {
			Context ctx = new InitialContext();
			DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/orderDS");
			return ds;
		} catch (NamingException e) {			
			return null;
		}

	}
	
	public Order getOrder(String id) {
		Order result = new Order();
		SqlSession session = getSqlSessionFactory().openSession();
		try {
			OrderMapper mapper=session.getMapper(OrderMapper.class);
			result = mapper.getOrder(id);
			session.commit();
		} finally {
		  session.close();
		}
		return result;
	}
	
}
